//
//  ConfigTableViewController.m
//  CaiPiao
//
//  Created by zhoup on 16/5/25.
//  Copyright © 2016年 zhoup. All rights reserved.
//

#import "ConfigTableViewController.h"

@interface ConfigTableViewController ()

@property(nonatomic,strong) NSArray *dataArray;
@end

@implementation ConfigTableViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    }
    return self;
}

-(NSArray *)dataArray
{
    if (_plistName == nil) {
        return  nil;
    }
    if (!_dataArray) {
        NSString *path = [[NSBundle mainBundle] pathForResource:_plistName ofType:@"plist"];
        
        _dataArray = [NSArray arrayWithContentsOfFile:path];
        
    }
    return _dataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%@",self.dataArray);
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSDictionary *dict =  self.dataArray[section];
    NSArray *items =  dict[@"items"];
    return items.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identify = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
    }
    NSDictionary *dict =  self.dataArray[indexPath.section];
    NSArray *items =  dict[@"items"];
    cell.textLabel.text = items[indexPath.row][@"title"];
    cell.imageView.image = [UIImage imageNamed:items[indexPath.row][@"image"]];
    
    NSString *accessory = items[indexPath.row][@"accessory"];
    NSString *accessory_image = items[indexPath.row][@"accessory_image"];
    
    if (accessory.length > 0) {
        
        Class classView = NSClassFromString(accessory);
        
        id classViewObj = [[classView alloc]init];
        
        if ([classViewObj isKindOfClass:[UIImageView class]]) {
            UIImageView *tempImageView = (UIImageView *)classViewObj;
            tempImageView.image = [UIImage imageNamed:accessory_image];
            
            [tempImageView sizeToFit];
        }
        
        
        if ([classViewObj isKindOfClass:[UILabel class]]) {
            UILabel *label = (UILabel *)classViewObj;
            label.text = @"";
            [label sizeToFit];
        }

        NSLog(@"%@",classViewObj);
        
        cell.accessoryView = classViewObj;
    }
    else
    {
        cell.accessoryView = nil;
    }
    
    
    
    return cell;
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSDictionary *dict =  self.dataArray[section];
    NSString *header =  dict[@"header"];
    return header;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict =  self.dataArray[indexPath.section];
    NSArray *items =  dict[@"items"];
    
    NSString *targetVc = items[indexPath.row][@"targetVc"];
    NSString *targetVcPath = items[indexPath.row][@"targetVcPath"];

    if (targetVc.length > 0) {
        Class classTargetVc = NSClassFromString(targetVc);
        
        ConfigTableViewController *targetVcObj = (ConfigTableViewController *)[[classTargetVc alloc]init];
        
        targetVcObj.plistName = targetVcPath;
        
        targetVcObj.title = items[indexPath.row][@"title"];

        [self.navigationController pushViewController:targetVcObj animated:YES];
    }
    
    
}

@end
